package com.ruoyi.base.controller;

import com.ruoyi.base.domain.Subject;
import com.ruoyi.base.domain.dto.SubjectReqDTO;
import com.ruoyi.base.service.ISubjectService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 科目管理Controller
 * 
 * @author ruoyi
 * @date 2025-02-21
 */
@RestController
@RequestMapping("/subject")
public class SubjectController extends BaseController
{
    @Autowired
    private ISubjectService financialSubjectService;

    /**
    * @Description: 科目树
    * @Param: []
    * @return: com.ruoyi.common.core.web.domain.AjaxResult
    * @Author: Yuan
    */
    @GetMapping("/subjectTree")
    public AjaxResult subjectTree(){
        return AjaxResult.success(financialSubjectService.selectSubjectTree());
    }

    /**
    * @Description: 获取上级科目选择列表
    * @Param: []
    * @return: com.ruoyi.common.core.web.domain.AjaxResult
    * @Author: Yuan
    */
    @GetMapping("/parent/options")
    public AjaxResult parentOptions() {
        return AjaxResult.success(financialSubjectService.selectParentSubjectOptions());
    }

    /**
    * @Description: 科目列表
    * @Param: [subjectReqDto]
    * @return: com.ruoyi.common.core.web.page.TableDataInfo
    * @Author: Yuan
    */
    @GetMapping("/subjectList")
    public TableDataInfo subjectList(SubjectReqDTO subjectReqDto){
        startPage();
        List<Subject> list = financialSubjectService.selectSubjectList(subjectReqDto);
        return getDataTable(list);
    }

    /**
     * 查询科目管理列表
     */
    @RequiresPermissions("voucher:subject:list")
    @GetMapping("/list")
    public TableDataInfo list(Subject financialSubject)
    {
        startPage();
        List<Subject> list = financialSubjectService.selectFinancialSubjectList(financialSubject);
        return getDataTable(list);
    }

    /**
     * 导出科目管理列表
     */
    @RequiresPermissions("voucher:subject:export")
    @Log(title = "科目管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Subject financialSubject)
    {
        List<Subject> list = financialSubjectService.selectFinancialSubjectList(financialSubject);
        ExcelUtil<Subject> util = new ExcelUtil<Subject>(Subject.class);
        util.exportExcel(response, list, "科目管理数据");
    }

    /**
     * 获取科目管理详细信息
     */
    @RequiresPermissions("voucher:subject:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(financialSubjectService.selectFinancialSubjectById(id));
    }

    /**
     * 新增科目管理
     */
    @RequiresPermissions("voucher:subject:add")
    @Log(title = "科目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid Subject financialSubject)
    {
        return toAjax(financialSubjectService.insertFinancialSubject(financialSubject));
    }

    /**
     * 修改科目管理
     */
    @RequiresPermissions("voucher:subject:edit")
    @Log(title = "科目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Valid Subject financialSubject)
    {
        return toAjax(financialSubjectService.updateFinancialSubject(financialSubject));
    }

    /**
     * 删除科目管理
     */
    @RequiresPermissions("voucher:subject:remove")
    @Log(title = "科目管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(financialSubjectService.deleteFinancialSubjectByIds(ids));
    }
}
